package com.txby.pf.modules.sys.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformApi.component.web.CommonController;
import com.txby.platformCore.entity.SysDict;

/**
 * 数据字典控制层
 */
@RestController
@RequestMapping("/sys/dict")
public class SysDictController extends CommonController {

	/**
	 * 字段列表
	 */
	@RequiresPermissions("sys:dict:list")
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="SELECT t.* FROM  "+this.mBaseDao.tableName(SysDict.class)+" t WHERE 1=1 " ;
		String dictName=params.get("dictName")==null?"":params.get("dictName").toString();
		if(!ChkUtil.isEmptyAllObject(dictName)){
			if (dictName.contains("%") || dictName.contains("_")) {
				dictName = dictName.trim().replace("%", "\\%").replace("_", "\\_");
				dictName = "%" + dictName + "%";
				sql += " and t.dictName like #{pojo.dictName} escape '\\'";
				params.put("dictName", "%" + dictName.trim() + "%");
			} else {
				dictName = "%" + dictName.trim() + "%";
				sql += " and t.dictName like #{pojo.dictName} ";
				params.put("dictName", "%" + dictName.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by updateTime ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}


	/**
	 * 获取一条信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:dict:info")
	public R info(@PathVariable("id") String id){
		String sql="SELECT t.*  FROM  "+this.mBaseDao.tableName(SysDict.class)+" t WHERE  t.id='"+id+"'" ;
		Map dict =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("dict", dict);
	}
	
	/**
	 * 保存库存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:dict:save")
	public R save(@RequestBody SysDict dict){
		try {
			ValidatorUtils.validateEntity(dict);
			String sql="select * from "+this.mBaseDao.tableName(SysDict.class)+" t where t.dictNo='"+dict.getDictNo()+"' and t.type='"+dict.getType()+"'";
			SysDict old=(SysDict) this.mBaseDao.findBySqlUniqueObject(sql, SysDict.class, null);
			 if(old!=null){
				 return R.ok().error("字典编码重复");
			 }
			 dict.setCreater(getUserId().toString());
			 dict.setUpdater(getUserId().toString());
			 this.mBaseDao.saveObject(dict);
		} catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 修改库存
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:dict:update")
	public R update(@RequestBody  SysDict dict){
		try {
			ValidatorUtils.validateEntity(dict, AddGroup.class);
			String sql="select * from "+this.mBaseDao.tableName(SysDict.class)+" where dictNo ='"+dict.getDictNo()+"' and id_!="+dict.getId_()+" and type='"+dict.getType()+"'";
			SysDict old=(SysDict) this.mBaseDao.findBySqlUniqueObject(sql, SysDict.class, null);
			if(old!=null){
				return R.ok().error("患者编号重复");
			}else{
				old=(SysDict) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(SysDict.class)+" where   id_="+dict.getId_(), SysDict.class, null);
				if(old!=null){
					this.mBaseDao.updateObject(dict);
				}
			}
			
		}catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	
	/**
	 * 删除库存
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:dict:delete")
	public R delete(@RequestBody String[] ids){
		 for(String id:ids){
		    	this.mBaseDao.executeSql(" delete from "+this.mBaseDao.tableName(SysDict.class)+" where id='"+id+"'", null);
		  }
		return R.ok();
	}
	
	
	/**
	 * 
	* @Title: getInfoByType 
	* @Description: 根据type获取码值及名称
	* @param @param type : 分类类型码
	* @param @param orderType DESC降序， ASC升序 默认升序  不区分大小写
	* @param @return    设定文件 
	* @return R    返回类型 
	* @throws
	 */
	@RequestMapping("/getInfoByType")
	public R getInfoByType(@RequestParam String type,String orderType){
/*		String type="";
		if(params==null || StringUtils.isBlank(type=(String) params.get("type"))) {
			return R.ok().put("dict", "");
		}*/
		if(StringUtils.isBlank(type)) {
			return R.ok().put("dict", "");
		}
		if(StringUtils.isBlank(orderType)) {
			orderType="ASC";
		}
		String sql="SELECT t.*  FROM  "+this.mBaseDao.tableName(SysDict.class)+" t WHERE  t.type='"+type+"' and status=0 order by sort "+ orderType;
		List dict=mBaseDao.findBySql(sql, Map.class, null);
		return R.ok().put("dict", dict);
		
	}
}
